/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.processes.common;

import java.util.Map;

import org.peony.models.MCol;
import org.peony.standard.ui.event.SIDataEventManager;
import org.peony.standard.unvarying.client.ComponentsMapKeys;
import org.peony.tools.ReturnMap;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: CommonCheckForColumn.java, v 0.000 2008-5-31 下午02:44:52 陈慧然 Exp
 */
public class CommonCheckForColumn{

	/**
	 * 校验空值
	 * @param id
	 * 		字段ID
	 * @param value
	 * 		字段值
	 * @return
	 * 		ReturnMap
	 */
	public static ReturnMap checkNull(MCol col, String value) {
		
		if(col != null){
			boolean canBeSpaces = col.isCanBeSpaces();
			
			if(!col.isNullable() &&
					((canBeSpaces && StringUtil.isEmpty(value)) ||
					(!canBeSpaces && StringUtil.isEmpty(value == null ? null : value.trim())))){
				return ReturnMap.showError(getErrorMsgNull(col));
			}
		}
		
		ReturnMap rm = ReturnMap.getInstance();
		rm.putIs_ok(ReturnMap.Y);
		return rm;
	}
	/**
	 * 校验长度
	 * @param col
	 * 		数据项Model MCol
	 * @param value
	 * 		字段值
	 * @return
	 * 		ReturnMap
	 */
	public static ReturnMap checkLength(MCol col, String value){
		
		if(col != null){
			if(StringUtil.isNotEmpty(value)){
				int length = col.isCanBeSpaces()?value.length():value.trim().length();
				if(length > col.getMaxLength()){
					return ReturnMap.showError(getErrorMsgMaxLength(col));
				}
				if(length < col.getMinLength()){
					return ReturnMap.showError(getErrorMsgMinLength(col));
				}
			}
		}
		
		ReturnMap rm = ReturnMap.getInstance();
		rm.putIs_ok(ReturnMap.Y);
		return rm;
	}
	
	
	private static String getErrorMsgNull(MCol col){
		return "\"" + col.getShowName() + "\" 不能为空！";
	}
	private static String getErrorMsgMaxLength(MCol col){
		return "\"" + col.getShowName() + "\" 的长度不能大于" + col.getMaxLength() +"！";
	}
	private static String getErrorMsgMinLength(MCol col){
		return "\"" + col.getShowName() + "\" 的长度不能小于" + col.getMinLength() +"！";
	}
}
